<template>
  <div class="app-container" style="height: 100%">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline :class="{ disabled: disableAction }" @submit.native.prevent>
      <el-form-item>
        <!-- <el-button icon="el-icon-refresh" @click="resetQuery">刷新</el-button> -->
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditItem.open()" v-if="showBtns.add">新增配置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100% - 100px)">
      <el-table-column label="配置类型" show-overflow-tooltip align="center" min-width="100">
        <dict-tag slot-scope="{ row }" :options="dict.type.patrol_cfg_type" :value="row.cfgType" />
      </el-table-column>
      <el-table-column label="所属分类" prop="patrolTypeName" show-overflow-tooltip min-width="200" />
      <el-table-column label="重复次数" prop="showCycleName" min-width="100" />
      <el-table-column label="配置描述" prop="remark" show-overflow-tooltip min-width="300" />
      <el-table-column label="是否为关键工序" align="center" width="120">
        <dict-tag slot-scope="{ row }" :options="yesOrNo" :value="row.isKp" />
      </el-table-column>
      <el-table-column label="状态" align="center" width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.status ? 'primary' : 'info'">{{ row.status ? '启用' : '禁用' }}</el-tag>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-turn-off" :loading="row.isEnableLoading" @click="toggleIsEnable(row)" v-if="row.showDisableBtn">禁用</el-button>
          <el-button size="mini" type="text" icon="el-icon-open" :loading="row.isEnableLoading" @click="toggleIsEnable(row)" v-else-if="row.showEnableBtn">启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 巡更配置列表
import auth from '@/plugins/auth'
import { yesOrNo } from '@/utils/baseData'
import { listPatCfg, setPatCfg } from '@/api/cq/patrol'
import { SYS_TASK, cycleTypeList } from './utils/common'
import EditItem from './components/EditPatCfg'
const cycleType2label = cycleTypeList.reduce((o, i) => !void (o[i.value] = i.label) && o, {})

export default {
  name: 'PatCfgList',
  components: { EditItem },
  dicts: ['patrol_cfg_type'],
  data() {
    return {
      yesOrNo,
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      showEditItem: false
    }
  },
  computed: {
    disableAction() {
      return this.listLoading
    },
    showBtns() {
      // 操作按钮权限
      const permCode = 'cq:patrol:config'
      const edit = auth.hasPermi(`${permCode}:edit`)
      const add = auth.hasPermi(`${permCode}:add`)
      const remove = auth.hasPermi(`${permCode}:remove`)
      const info = auth.hasPermi(`${permCode}:info`)
      const enable = auth.hasPermi(`${permCode}:enable`)
      const disable = auth.hasPermi(`${permCode}:disable`)
      return { edit, add, remove, info, enable, disable }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const { showBtns } = this
      this.listLoading = true
      const res = await listPatCfg(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.rows || []
      data.map(i => {
        i.isEnableLoading = false
        i.showDisableBtn = showBtns.disable && i.status
        i.showEnableBtn = showBtns.enable && !i.status

        const cycleCount = i.cycleCount === 1 ? '' : i.cycleCount
        i.showCycleName = i.cfgType === SYS_TASK ? '' : `每${cycleCount}${cycleType2label[i.cycleType]}${i.times}次`
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async toggleIsEnable(item) {
      const isEnable = item.status ? 0 : 1
      const name = isEnable === 1 ? '启用' : '禁用'
      const res1 = await this.$confirm(`确认${name}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      const opt = {
        ...item,
        // patCfgId: item.patCfgId,
        status: isEnable
      }
      item.isEnableLoading = true
      const res = await setPatCfg(opt).catch(() => false)
      item.isEnableLoading = false
      if (res === false) return

      this.$message.success(`已${name}`)
      this.getList()
    }
  }
}
</script>
